package com.hex.liyue.launchdatemodule.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hex.liyue.R;
import com.hex.liyue.commonmodule.utils.DateUtils;
import com.hex.liyue.commonmodule.utils.L;
import com.hex.liyue.launchdatemodule.bean.LaunchListResultBean;

import java.util.List;

/**
 * Created by WJ on 2016/6/12.
 */
public class AdapterLaunch extends RecyclerView.Adapter<AdapterLaunch.MyHolder> {
    private List<LaunchListResultBean.DataBean> list;

    public AdapterLaunch(List<LaunchListResultBean.DataBean> list) {
        this.list = list;
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClickListener(String appointment_id);

        void onLongClickListener(AdapterLaunch.MyHolder viewHolder, String appointment_id);
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(parent.getContext(), R.layout.item_launch_list, null);
        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(final MyHolder holder, final int position) {
        final LaunchListResultBean.DataBean dataBean = list.get(position);
        holder.tv_location.setText(dataBean.location + " " + dataBean.event);
        long invitations_time = dataBean.invitations_time * 1000;
        long current_time = System.currentTimeMillis();
        boolean isFinish = current_time > invitations_time;//是否结束

        holder.v_red_point.setVisibility(View.INVISIBLE);
        if (dataBean.status.equals("-1")) {
            //取消了约会
            holder.tv_info.setText("您已取消了本次约会");
            holder.tv_time.setText(DateUtils.getFormatDate(invitations_time, "yyyy年MM月dd日 EEEE HH:mm") + " 已结束");
            return;
        }

        if (isFinish) {
            //结束了
            holder.tv_time.setText(DateUtils.getFormatDate(invitations_time, "yyyy年MM月dd日 EEEE HH:mm") + " 已结束");
            if (dataBean.apply_list == null || dataBean.apply_list.size() <= 0) {
                //无人应约
                holder.tv_info.setText("当前约会没有收到礼物，长按可以进行关闭操作");
                holder.canCencal = true;
            } else {
                //有人应约
                int all_person_num = dataBean.apply_list.size();//几个应约者
                int agree_person_num = 0;//同意了几个应约者
                int reject_person_num = 0;//拒绝了几个应约者
                int wait_person_num = 0;//几个应约者等待处理
                int success_person_num = 0;//约会完成，对方收取礼物
                for (LaunchListResultBean.DataBean.ApplyListBean applyListBean : dataBean.apply_list) {
                    if (applyListBean.status.equals("1")) {
                        agree_person_num++;
                    }
                    if (applyListBean.status.equals("2")) {
                        reject_person_num++;
                    }
                    if (applyListBean.status.equals("3")) {
                        wait_person_num++;
                    }
                    if (applyListBean.status.equals("4")) {
                        success_person_num++;
                    }
                }
                if (agree_person_num == success_person_num) {
                    //所有人确认完成
                    holder.tv_info.setText("应约者们已经确认本次约会，礼物已经躺在你的礼物中心");
                } else {
                    holder.tv_info.setText("等待" + (success_person_num - agree_person_num) + "应约者确认本次约会");
                }
                if (reject_person_num == dataBean.apply_list.size()) {
                    //拒绝了所有
                    holder.tv_info.setText("您已经拒绝了本次约会所有应约者的礼物");
                    holder.canCencal = true;
                }
            }
        } else {
            //未结束
            holder.tv_time.setText(DateUtils.getFormatDate(invitations_time, "yyyy年MM月dd日 EEEE HH:mm") + " 参与中");
            if (dataBean.apply_list == null || dataBean.apply_list.size() <= 0) {
                //无人应约
                holder.tv_info.setText("当前约会没有收到礼物，长按可以进行关闭操作");
                holder.canCencal = true;
            } else {
                //有人应约
                int all_person_num = dataBean.apply_list.size();//几个应约者
                int agree_person_num = 0;//同意了几个应约者
                int reject_person_num = 0;//拒绝了几个应约者
                int wait_person_num = 0;//几个应约者等待处理
                for (LaunchListResultBean.DataBean.ApplyListBean applyListBean : dataBean.apply_list) {
                    if (applyListBean.status.equals("1")) {
                        agree_person_num++;
                    }
                    if (applyListBean.status.equals("2")) {
                        reject_person_num++;
                    }
                    if (applyListBean.status.equals("3")) {
                        wait_person_num++;
                    }
                }
                if (wait_person_num == dataBean.apply_list.size()) {
                    holder.v_red_point.setVisibility(View.VISIBLE);
                }
                if (reject_person_num == dataBean.apply_list.size()) {
                    //拒绝了所有
                    holder.tv_info.setText("您已经拒绝了本次约会所有应约者的礼物");
                    holder.canCencal = true;
                } else {
                    if (agree_person_num != 0) {
                        //有同意的
                        holder.tv_info.setText("有" + all_person_num + "个应约者,同意了" + agree_person_num+"个");
                    } else {
                        //没有同意的
                        holder.tv_info.setText("有" + all_person_num + "个应约者");
                    }
                }
            }
        }

        holder.rl_launch_box.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onItemClickListener(dataBean.appointment_id);
            }
        });
        holder.rl_launch_box.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                //长按删除约会
                if (holder.canCencal) {
                    L.d("WJ", "可以删除约会");
                    onItemClickListener.onLongClickListener(holder, dataBean.appointment_id);
                } else {
                    L.d("WJ", "不可以删除约会");
                }
                return true;
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class MyHolder extends RecyclerView.ViewHolder {
        RelativeLayout rl_launch_box;
        TextView tv_location, tv_time;
        View v_red_point;
        public boolean canCencal = false;
        public TextView tv_info;


        public MyHolder(View itemView) {
            super(itemView);
            rl_launch_box = (RelativeLayout) itemView.findViewById(R.id.rl_launch_box);
            tv_location = (TextView) itemView.findViewById(R.id.tv_location);
            tv_time = (TextView) itemView.findViewById(R.id.tv_time);
            tv_info = (TextView) itemView.findViewById(R.id.tv_info);
            v_red_point = itemView.findViewById(R.id.v_red_point);
        }

    }
}
